<?php

$type = (int) $_REQUEST["type"];
 require_once '../config/common.php';

if (isset($_REQUEST['act']) && $_REQUEST['act'] == 'add') {
//
    try {
        $title = test_input($_REQUEST['title']); //接收传值 防止插入脚本语言
        $content = $_REQUEST['content'];
        if (empty($title)) {//验证输入标题是否为空 
            throw new Exception("请输入标题 ");
        }
        if (empty($content)) {//验证输入内容是否为空
            throw new Exception("请输入内容 ");
        }
        $size = $_FILES['file']['size']; //获取上传文件大小
        if ($size === 0) {//大小为零，判断为未上传
            throw new Exception("不允许上传空文件");
        }
        if ($size > 2048 * 1024) {//限制上传文件的尺寸
            throw new Exception("文件大小不得大于2M");
        }
        $allow = array("png", "jpg", "jpeg", "gif"); //限制上传文件类型
        $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION); //获取上传文件类型  
        if (!in_array($ext, $allow)) {//判断上传文件类型是否在规定类型内
            throw new Exception('不允许的图像格式');
        }
        require_once '../config/config.php';   //包含并运行指定文件
        $path = ROOT . '/admin/upload/' . date('y/m/d/', time()); //创建储存图片的文件夹
        if (!file_exists($path) && !mkdir($path, 0777, true)) {
            throw new Exception('创建目录失败');
        }
        $destination = $path . md5(time() . mt_rand(1, 99999)) . '.' . $ext; //上传图片命名
        if (!move_uploaded_file($_FILES['file']['tmp_name'], $destination)) {//移动临时文件
            throw new Exception('移动临时文件失败');
        }
        require_once '../config/img.inc.php'; //执行图片缩放函数
        if ($type == 0 || $type == 1 || $type == 2) {
            $w = 235;
            $h = 235;
            suofang($destination, $w, $h); //定义图片缩放
        } elseif ($type == 3) {
            suofang($destination, 350, 337); //定义图片缩放
            suofang($destination, 300, 133); //定义图片缩放
        } else {
            exit("请核对图片尺寸");
        }
        $src = str_replace(ROOT, '', $destination); //获取缩放图片路径
        $conn = new mysqli(HOST, USER, PASSWORD, DATABASE);  //连接数据库
        if ($conn->connect_errno) {
            throw new Exception('连接数据库失败' . $conn->connect_error);
        }
        $stmt = $conn->prepare("insert into media_list(type,content,title,time,img) values(?,?,?,?,?)"); //准备上传文件写入数据库的命令
        $time = time();
        $stmt->bind_param('issis', $type, $content, $title, $time, $src); //绑定参数  
        $stmt->execute(); //执行
        if ($stmt->errno) {
            throw new Exception('插入数据失败');
        }
        echo "<script>alert('上传成功,点击返回');window.location='media.php?type=$type'</script>";
    } catch (Exception $exc) {
        echo $exc->getMessage();
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="" method="post" enctype="multipart/form-data">
            <select name="type">
                <option value="0" <?php echo $type == 0 ? 'selected="selected"' : ''; ?>>视频</option>
                <option value="1" <?php echo $type == 1 ? 'selected="selected"' : ''; ?>>音乐</option>
                <option value="2" <?php echo $type == 2 ? 'selected="selected"' : ''; ?>>照片</option>
                <option value="3" <?php echo $type == 3 ? 'selected="selected"' : ''; ?>>资讯</option>
            </select>
            <br>
            <input type="text" name="title"/><span>文章标题</span>
            <br>
            <input type="file" name="file"/><span>选择封面图片</span>
            <br>
            <input type="hidden" name="type" value="<?php echo $type; ?>"/>
            <input type="hidden" name="act" value="add"/>
            <script id="container" name="content" type="text/plain"></script>
            <input type="submit" value="发表">
            <input type="reset" value="清空">
        </form>
        <script type="text/javascript" src="../ueditor/ueditor.config.js"></script>
        <!-- 编辑器源码文件 -->
        <script type="text/javascript" src="../ueditor/ueditor.all.js"></script>
        <!-- 实例化编辑器 -->
        <script type="text/javascript">
            var ue = UE.getEditor('container');
        </script>
    </body>
</html>